import * as Api from '../../utils/api.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColors:'',
    hasGoneAway: false,
    classifyBanner: '',
    categoryList: [],
    carId: '',
    carInfo: [],
    defaultCarId: '',
    windowWidth: 375,
    goodsCount:[]
  },


  getCarInfo() {
    let self = this
    let url = app.globalData.baseUrl + '/bsm'
    let data = {
      modelId: this.data.carId,
      merchantId: app.globalData.merchant.merchantId
    }
    //发起网络请求
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }

      if (res.statusCode != 200) {
        console.log("请求数据错误");
        return;
      }
      if (res.statusCode === 200) {
        self.setData({
          carInfo: res.data.data
        })

      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  existing(e) {
    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      
      wx.redirectTo({
        url: '../filter_type/filter_type?carId=' + this.data.carId,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  productList(e) {
    let self = this

    if (this.data.goodsCount[e.currentTarget.dataset.main.catId] === 0 || this.data.goodsCount[e.currentTarget.dataset.main.catId] === '' || !this.data.goodsCount[e.currentTarget.dataset.main.catId]) {
      return
    }
 
      
    if (this.data.goodsCount[e.currentTarget.dataset.main.catId] === 1){
        let catId = ''
        let secCatId = ''

      if (e.currentTarget.dataset.main.levelNum === 1){
        catId = e.currentTarget.dataset.main.catId
      }else{
        catId = e.currentTarget.dataset.main.parentId
          secCatId = e.currentTarget.dataset.main.catId


      }
      let url = app.globalData.baseUrl1 + '/goods/front/list'
      let data = {

      }
      if (app.globalData.userId == null) {
        data = {
          merchantId: app.globalData.merchant.merchantId,
          firstCatId: catId,//一级分类id
          secCatIds: secCatId,//二级分类id
          carId: this.data.carId
        }
      } else {
        data = {
          merchantId: app.globalData.merchant.merchantId,
          userId: app.globalData.userId,
          firstCatId: catId,//一级分类id
          secCatIds: secCatId,//二级分类id
          carId: this.data.carId
        }
      }


      //发起网络请求
      let callBack = (res) => {
        if (res == null) {
          console.log("网络请求错误！");
          return;
        }

        if (res.statusCode != 200) {
          console.log("请求数据错误");
          return;
        }
        if (res.statusCode === 200) {
          console.log('商品列表', res)
                
          if (res.data.data.length>0){
            if (!this.data.hasGoneAway) {
              this.setData({ hasGoneAway: true })
                  wx.navigateTo({
                    url: '../product_datil/product_datil?id=' + res.data.data[0].goodsId + '&carId=' + this.data.carId,
                    success: function (res) { },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
            }
                  
          }

        }


      }
      Api.reqFormPost(url, data, callBack)
        
return
    }

   
    let url = '../product_list/product_list?id=' + this.data.carId + "&levelNum=" + e.currentTarget.dataset.main.levelNum + "&catname=" + e.currentTarget.dataset.main.label
    if (e.currentTarget.dataset.main.levelNum === 1) {
      url = url + "&catid=" + e.currentTarget.dataset.main.catId
    } else {

      
      url = url + "&catid=" + e.currentTarget.dataset.main.parentId + "&secCatId=" + e.currentTarget.dataset.main.catId
    }


    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })

    wx.navigateTo({
      url: url,
      success: function (res) {
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    }
  },
  getCategoryList() {

    let self = this
    let url = app.globalData.baseUrl + '/merchant/catlist'
    let data = {
      merchantId: app.globalData.merchant.merchantId,
      modelId: this.data.carId
    }

    //发起网络请求
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }
      if (res.statusCode != 200) {
        console.log("请求数据错误");
        wx.showModal({
          title: '',
          content: '网络加载失败, 请检查您的网络',
          showCancel: false,
          cancelText: '',
          cancelColor: '',
          confirmText: '知道了',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        return;
      }
      if (res.statusCode === 200) {
        self.setData({
          categoryList: res.data.data.children,
          goodsCount: res.data.data.goodsCount
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  getNameByUserId() {
    console.log('默认首选车型ID', app.globalData.carId)
    if ( app.globalData.carId !== null) {
      this.setData({
        carId: app.globalData.carId
      })
      this.getCarInfo()
      this.getCategoryList()
      return
    } else {
      this.getCategoryList()
    }
    let self = this
    let url = app.globalData.baseUrl1 + '/supplier/info/byuser'
    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId,
    }
    let callBack = (res) => {
      if (res == null) {
        console.log("网络请求错误1！");
        return;
      }
      if (res.data.status != 200) {
        console.log("请求数据错误1");
        return;
      }
      if (res.data.status == 200) {

        self.setData({
          carId: res.data.data.defaultCarId
        })
        if (self.data.carId) {
          self.getCarInfo()
        }

        self.getCategoryList()
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    let self = this
    let classifyBanner = app.globalData.merchant.classifyBanner.split(",")
    self.setData({
      windowWidth: app.globalData.windowWidth,
      classifyBanner: classifyBanner,
      themeColors: app.globalData.merchant.themeColors
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    if (app.globalData.merchant.mustLogin && app.globalData.userId === null) {
      wx.navigateTo({
        url: '/pages/core/login/login',
        success() {
          wx.showToast({
            title: '请先登陆',
            icon: 'loading',
            duration: 1000
          })
        }
      })
      return
    }
    let self = this
    wx.setNavigationBarTitle({
      title: app.globalData.merchant.merchantName
    })
    self.setData({ hasGoneAway: false })
    self.getNameByUserId()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})